#include <cmath>
#include <iostream>
using namespace std;

const int kMaxN = 10;
int n;
int s[kMaxN], b[kMaxN];
int ans = 1e9, used = 0;

void DFS(int now = 0, int nows = 1, int nowb = 0, int used = 0) {
  if (now == n) {
    if (used > 0) {
      ans = min(ans, abs(nows - nowb));
    }
    return;
  }
  DFS(now + 1, nows, nowb, used);
  DFS(now + 1, nows * s[now], nowb + b[now], used + 1);
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> b[i];
  }
  DFS();
  cout << ans << "\n";
  return 0;
}